<?php
class model_transporttmp extends framework_base_model {
	/**
	 * @desc 添加地区
	 * @param array 
	 * @return number
	 */
	public static function transporttmp_add($data) {
    	if(!is_array($data)) {
    		return 0;
    	}
    	try{
    		$sql = "insert into `weixin_transport_tmp` set ";
    		$binds = array();
    		foreach ($data as $k => $v) {
    			$sql .= '`' . $k . '`=:' . $k . ',';
    			$binds[':' . $k] = $v;
    		}
    		$sql .= 'raw_add_time=NOW()';
    		$arr=self::DB() -> dbh -> prepare($sql);
    		$rel = $arr->execute($binds);
    		if($rel == TRUE) {
    			return self::DB()->dbh->lastInsertId();
    		} else {
    			return 0;
    		}
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return 0;
    	}
    }
	/*
     * 判断该运输模板是否还存在
     */
    public static function transporttmp_exists($tmp_name, $tmp_type, $id = 0) {
    	$info = array();

    	$sql = "select `transport_tmp_id` from `weixin_transport_tmp` where `tmp_name` = :tmp_name and `tmp_type` = :tmp_type and `delete_flag` = 0 ";
		$binds[":tmp_name"] = $tmp_name;
		$binds[":tmp_type"] = $tmp_type;
		if(!empty($id)){
			$sql .= " and `transport_tmp_id` != :transport_tmp_id";
			$binds[":transport_tmp_id"] = $id;
		}
    	try {
    		$st = self::DB() -> dbh -> prepare($sql);
    		$st -> execute($binds);
    		$info = $st -> fetch();
    	} catch (Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    	}
    	return $info;
    }
	/*
     * 根据id是否存在该分类名称
     */
    public static function get_transporttmp_by_id($id) {
    	$info = array();
		$id = intval($id);
    	if(empty($id)) {
    		return $info;
    	}
    	$sql = "select `transport_tmp_id`,`tmp_name`,`section`,`tmp_type`,`author`,`editer`,`raw_add_time`,`raw_update_time` from `weixin_transport_tmp` where `transport_tmp_id`=:transport_tmp_id and delete_flag = 0";
    	try {
    		$st = self::DB() -> dbh -> prepare($sql);
    		$st -> execute(array(":transport_tmp_id" => $id));
    		$info = $st -> fetch();
    	} catch (Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    	}
    	return $info;
    }
    /**
     * @desc 更新分类
     * @param unknown $id
     * @param unknown $data
     * @return unknown|number
     */
    public static function update_transporttmp_info($id,$data) {
    	try{
    		$sql = 'UPDATE `weixin_transport_tmp` SET ';
    		$binds = array();
    		foreach ($data as $k => $v) {
    			$sql .= '`' . $k . '`=:' . $k . ',';
    			$binds[':' . $k] = $v;
    		}
    		$ids = implode(',',$id);
    		$sql = trim($sql,',');
    		$sql .= ' WHERE `transport_tmp_id` in ('.$ids.')';
    		$stm=self::DB() -> dbh -> prepare($sql);
    		$rel = $stm->execute($binds);
    		$rowCount = $stm -> rowCount();
    		return true;
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		return -1;
    	}
    }
    /**
     * 获取大分类列表
     * @param number $nextNum
     * @param number $pagesize
     * @param unknown $condition
     * @return unknown|Ambigous <number, unknown>
     */
    public static function get_transporttmp_list($nextNum = 0,$pagesize = 0,$condition = array()) {
    	$sql = "select a.`transport_tmp_id`,a.`tmp_name`,a.`tmp_type`,a.`author`,a.`editer`,a.`raw_add_time`,a.`raw_update_time`,b.`transport_price_id` from `weixin_transport_tmp` as a left join `weixin_transport_price` as b on a.`transport_tmp_id`=b.`transport_tmp_id` where a.`delete_flag` = 0 ";
		$where = "";
    	if(is_array($condition)&& count($condition)>0){
    		if(isset($condition['tmp_name'])) {
    			$where .= " and a.`tmp_name` like '%{$condition['tmp_name']}%'";
    		}
			if(isset($condition['tmp_type'])) {
    			$where .= " and a.`tmp_type` = {$condition['tmp_type']}";
    		}
    	}
    	$where .= " group by a.`tmp_name` order by a.`transport_tmp_id` desc";
    	$limit = "";
        if($pagesize != 0 ){
    		$limit = " limit {$nextNum},{$pagesize}";
    	}
    	$result = array();
    	try {
    		$st = self::DB()->dbh->prepare($sql.$where.$limit);
    		$st->execute();
    		$result = $st->fetchAll();
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		$result = array();
    	}
		$where = "";
    	if(is_array($condition)&& count($condition)>0){
    		if(isset($condition['tmp_name'])) {
    			$where .= " and `tmp_name` like '%{$condition['tmp_name']}%'";
    		}
			if(isset($condition['tmp_type'])) {
    			$where .= " and `tmp_type` = {$condition['tmp_type']}";
    		}
    	}
		$where .= " order by `transport_tmp_id` desc";
		$sql = "select count(`transport_tmp_id`) from `weixin_transport_tmp` where `delete_flag` = 0 {$where}";
		$binds2 = array();
    	try {
    		$st = self::DB()->dbh->prepare($sql);
    		$st->execute($binds2);
    		$count = $st->fetchColumn();
    		$result['count'] = $count;
    	}catch(Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    		$result['count'] = 0;
    	}		
		return $result;
    }
	//根据ID获取模板
	public static function get_transporttmp_by_type($tmp_type) {
    	$info = array();
		$tmp_type = intval($tmp_type);
    	if(empty($tmp_type)) {
    		return $info;
    	}
    	$sql = "select `transport_tmp_id`,`tmp_name` from `weixin_transport_tmp` where `tmp_type`=:tmp_type and delete_flag = 0";
    	try {
    		$st = self::DB() -> dbh -> prepare($sql);
    		$st -> execute(array(":tmp_type" => $tmp_type));
    		$info = $st -> fetchAll();
    	} catch (Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    	}
    	return $info;
    }
	//获取运输方式存在的模板
	public static function get_tmp_by_transport($type, $area_id){
		$info = array();
		$type = intval($type);
		$area_id = intval($area_id);
    	if(empty($type) || empty($area_id)) {
    		return $info;
    	}
    	$sql = "select * from `weixin_transport_price` where `transport_tmp_id` in (select `transport_tmp_id` from `weixin_transport_tmp` where `tmp_type` = {$type}) and `area_id` = :area_id and `delete_flag` = 0";
    	try {
    		$st = self::DB() -> dbh -> prepare($sql);
    		$st -> execute(array(':area_id'=>$area_id));
    		$info = $st -> fetchAll();
    	} catch (Exception $e){
    		framework_static_function::write_log($e->getMessage(),'error_sql');
    	}
    	return $info;
	}
}
